package com.kaigejava.juc.toolsdemo.cyclcbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * @author kaigejava
 * cyclic
 */
public class CyclicBarrierDemo {

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            new Dragon().say();
        });
        for(int i = 1;i<=7;i++){
            final  int dragonBall = i;
            new Thread(()->{
                try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("==当前找到第"+dragonBall+"颗龙珠");
                cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },""+dragonBall).start();
        }
    }

}
